package com.fhz.day016.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fhz.day016.R;
import com.fhz.day016.entity.Song;
import com.fhz.day016.utils.SongDataUtil;

import java.util.List;

/**
 * 时间:2022/5/9
 *
 * @author msfeng
 * 简述: TODO
 */
public class SongAdapter extends BaseAdapter {

    List<Song> songList;
    private Context context;

    public SongAdapter(List<Song> songList, Context context) {
        this.songList = songList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return songList.size();
    }

    @Override
    public Object getItem(int position) {
        return songList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            // 引入布局
            view = View.inflate(context, R.layout.song_layout, null);
            // 实例化对象
            holder.iamge = view.findViewById(R.id.item_mymusic_image);
            holder.title = (TextView) view.findViewById(R.id.item_mymusic_title);
            holder.duration = (TextView) view.findViewById(R.id.item_mymusic_duration);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // 给控件赋值
        Song song = songList.get(position);

        Glide.with(context).load(SongDataUtil.getAlbum(context,Integer.parseInt(song.getAlbum()))).into(holder.iamge);

        String title = song.getTitle();
        //标题
        holder.title.setText(title);
        //时长
        holder.duration.setText(SongDataUtil.formatTime(Integer.parseInt(song.getDuration())));

        return view;
    }

    class ViewHolder {
        ImageView iamge;// 图片
        TextView title;// 歌手
        TextView duration;// 时长
    }

}

